<?php

namespace App\Http\Controllers;

use App\Helpers\LogHelper;
use App\Helpers\MvcHelper;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected array $params;  // 接口请求参数
    protected array $sysParams;  // 其他请求参数

    public function __construct()
    {
        /**
         * 将业务参数和系统参数放入基类的变量中
         */
        $this->params    = MvcHelper::getApiRequestParams();
        $this->sysParams = MvcHelper::getRequestSysParams();
    }

    /**
     * 规则验证：不抛出异常
     *
     * @param $rule
     * @return bool
     */
    protected function ruleCheck($rule): bool
    {
        $validator = Validator::make($this->params, $rule);

        if (!empty($validator->errors()->first())) {
            $message = [
                $this->params,
                $validator->errors()->first(),
            ];
            LogHelper::info('Validator', $message);
        }
        return $validator->fails();
    }
}
